package com.stylefeng.guns.modular.flagglory.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.Division;
import com.stylefeng.guns.modular.flagglory.service.IDivisionService;

import javax.validation.Valid;

/**
 * 段位控制器
 *
 * @author fengshuonan
 * @Date 2018-01-31 16:09:51
 */
@Controller
@RequestMapping("/division")
public class DivisionController extends BaseController {

    private String PREFIX = "/flagGlory/division/";

    @Autowired
    private IDivisionService divisionService;

    /**
     * 跳转到段位首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "division.html";
    }

    /**
     * 跳转到添加段位
     */
    @RequestMapping("/division_add")
    public String divisionAdd() {
        return PREFIX + "division_add.html";
    }

    /**
     * 跳转到修改段位
     */
    @RequestMapping("/division_update/{divisionId}")
    public String divisionUpdate(@PathVariable Integer divisionId, Model model) {
        Division division = divisionService.selectById(divisionId);
        model.addAttribute("item",division);
        LogObjectHolder.me().set(division);
        return PREFIX + "division_edit.html";
    }

    /**
     * 获取段位列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return divisionService.selectList(null);
    }

    /**
     * 新增段位
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@Valid Division division, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        divisionService.insert(division);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除段位
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer divisionId) {
        divisionService.deleteById(divisionId);
        return SUCCESS_TIP;
    }

    /**
     * 修改段位
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid Division division, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        divisionService.updateById(division);
        return super.SUCCESS_TIP;
    }

    /**
     * 段位详情
     */
    @RequestMapping(value = "/detail/{divisionId}")
    @ResponseBody
    public Object detail(@PathVariable("divisionId") Integer divisionId) {
        return divisionService.selectById(divisionId);
    }
}
